package cn.tedu.csmall.product.service.impl;

import cn.tedu.csmall.commons.exception.ServiceException;
import cn.tedu.csmall.commons.pojo.vo.PageData;
import cn.tedu.csmall.commons.util.PageDataConverter;
import cn.tedu.csmall.commons.web.ServiceCode;
import cn.tedu.csmall.product.mapper.*;
import cn.tedu.csmall.product.pojo.entity.SPU;
import cn.tedu.csmall.product.pojo.param.SPUInsertParam;
import cn.tedu.csmall.product.pojo.vo.BrandStandardVO;
import cn.tedu.csmall.product.pojo.vo.CategoryStandardVO;
import cn.tedu.csmall.product.pojo.vo.SPUListVO;
import cn.tedu.csmall.product.pojo.vo.SPUStandardVO;
import cn.tedu.csmall.product.service.SPUService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Random;

@Slf4j
@Service
public class SPUServiceImpl implements SPUService {
    @Autowired
    SPUMapper mapper;
    @Autowired
    BrandMapper brandMapper;
    @Autowired
    SKUMapper skuMapper;
    @Autowired
    CategoryMapper categoryMapper;
    @Override
    public void insert(SPUInsertParam SPUInsertParam) {
        log.debug("开始处理【添加相册】的业务，参数：{}", SPUInsertParam);
        QueryWrapper<SPU> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("name",SPUInsertParam.getName());
        int countByName = mapper.selectCount(queryWrapper);
        log.debug("根据相册名称统计匹配的相册数量，结果：{}", countByName);
        if(mapper.selectCount(queryWrapper)>0){
            String message="添加相册失败，相册名称已被占用!";
            log.warn(message);
            throw new ServiceException(message, ServiceCode.ERR_CONFLICT);
        }

        BrandStandardVO brand=brandMapper.getStandardById(SPUInsertParam.getBrandId());

        CategoryStandardVO category=categoryMapper.getStandardById(SPUInsertParam.getCategoryId());

        Long spuId=new Random().nextLong();
        SPU spu=new SPU();
        BeanUtils.copyProperties(SPUInsertParam,spu);
        spu.setId(spuId);
        // 补全属性值：brand_name, category_name >>> 此前检查时的查询结果
        spu.setBrandName(brand.getName());
        spu.setCategoryName(category.getName());
        // 补全属性值：sales, comment_count, positive_comment_count >>> 0
        spu.setSales(0);
        spu.setCommentCount(0);
        spu.setPositiveCommentCount(0);
        // 补全属性值：is_deleted, is_published, is_new_arrival, is_recommend >>> 0
        spu.setIsDeleted(0);
        spu.setIsPublished(0);
        spu.setIsNewArrival(0);
        spu.setIsRecommend(0);
        // 补全属性值：is_checked >>> 0, check_user >>> null, gmt_check >>> null
        spu.setIsChecked(0);
        log.debug("准备将新的相册数据写入到数据库，数据：{}", spu);
        int row = mapper.insert(spu);
        if(row !=1){
            String message="添加管理员失败，服务器忙，请稍后再试!";
            log.warn(message);
            throw new ServiceException(message, ServiceCode.ERR_INSERT);
        }
        log.debug("将新的相册数据写入到数据库，完成！");
    }



    @Override
    public SPUStandardVO getStandardById(Long id) {
        log.debug("开始处理【根据ID查询相册】的业务，参数：{}", id);
        if(mapper.getStandardById(id)==null){
            String message = "查询相册详情失败，相册数据不存在！";
            log.warn(message);
            throw new ServiceException( message,ServiceCode.ERR_NOT_FOUND);
        }
        return mapper.getStandardById(id);
    }

    @Override
    public PageData<SPUListVO> list(Integer pageNum, Integer pageSize) {
        log.debug("开始处理【查询相册】的业务，页数：{}，每页的数据量：{}",pageNum,pageSize);
        PageHelper.startPage(pageNum,pageSize);
        List<SPUListVO> list=mapper.list();
        PageInfo<SPUListVO> pageInfo=new PageInfo<>(list);
        PageData<SPUListVO> pageData= PageDataConverter.convert(pageInfo);
        return pageData;
    }

    @Override
    public PageData<SPUListVO> list(Integer pageNum) {
        Integer pageSize=5;
        return list(pageNum,pageSize);
    }

}
